package com.etc.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etc.entity.Media;
import com.etc.serviceimpl.MediaServiceImpl;
import com.etc.tools.Msg;
import com.etc.tools.PageData;

@Controller
public class MediaController {
	@Resource(name = "mediaServiceImpl")
	private MediaServiceImpl mediaServiceImpl;

	/**
	 * 分页＋模糊查询 ajax
	 * 
	 * @param page
	 *            页码
	 * @param limit
	 *            每页显示记录数
	 * @param content
	 *            模糊查询的关键字
	 * @return pageDate<media>对象
	 */
	@RequestMapping(value = "page", method = RequestMethod.GET)
	@ResponseBody
	public PageData<Media> listMedia(Integer page, Integer limit, String content) {
		if (content == null) {
			content = "";
		}
		System.out.println(content+"#########");
		PageData<Media> pd = mediaServiceImpl.getMediaByPage(page, limit, content);
		pd.setMsg("请求成功");
		pd.setCode(0);

		return pd;

	}

	/**
	 * 删除media的方法
	 * 
	 * @param mid
	 *            根据页面传回来的mid删除数据
	 * @return msg 删除成功或失败的消息
	 */
	@RequestMapping(value = "del/{mid}", method = RequestMethod.DELETE)
	@ResponseBody
	public Msg<String> DeleteMedia(@PathVariable("mid") int mid) {
		boolean flag = mediaServiceImpl.deleteByPrimaryKey(mid);
		Msg<String> msg = new Msg<>();
		msg.setMsg(flag ? "删除成功" : "删除失败");
		System.out.println(msg);
		System.out.println(msg.getMsg());
		return msg;
	}
	
}
